﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;

namespace TCDC_System.Content.TransectionData
{
    public partial class information_manage : SystemPage.UIPage
    {

        private tcdc_mst_information_group objInformationGroup;
        private tcdc_mst_information objInformation;

        protected void Page_Init(object sender, EventArgs e)
        {
            this.InformationGroupAdd1.GridStore = StoreInformationGroup;
            this.InformationManageAdd1.GridStore = StoreInformation;
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            objInformationGroup = new tcdc_mst_information_group();
            objInformationGroup.strConn = ConfigurationManager.AppSettings["ConnString"];
            objInformation = new tcdc_mst_information();
            objInformation.strConn = ConfigurationManager.AppSettings["ConnString"];
            if (!X.IsAjaxRequest)
            {
                StoreInformationGroup.DataSource = GetInformationGroup();
                StoreInformationGroup.DataBind();
            }
        }

        //InformationGroup

        protected DataTable GetInformationGroup()
        {
            DataTable dt = objInformationGroup.getByObject(new tcdc_mst_information_group());
            return dt;
        }

        protected void AddInformationGroup(object sender, DirectEventArgs e)
        {
            InformationGroupAdd1.Show();
        }

        protected void UpdateInformationGroup(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["InformationGroupValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string information_group_id = string.Empty;
            string information_group_desc = string.Empty;
            string object_type = string.Empty;
            string sorting_group_no = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                information_group_id = row["information_group_id"].ToString();
                information_group_desc = row["information_group_desc"].ToString();
                object_type = row["object_type"].ToString();
                sorting_group_no = row["sorting_group_no"].ToString();
            }

            objInformationGroup.information_group_id = Convert.ToInt32(information_group_id);
            objInformationGroup.information_group_desc = information_group_desc;
            objInformationGroup.object_type = object_type;
            objInformationGroup.sorting_group_no = Convert.ToInt32(sorting_group_no);
            objInformationGroup.updateData(objInformationGroup);
            StoreInformationGroup.DataSource = GetInformationGroup();
            StoreInformationGroup.DataBind();
        }
        protected void DeleteInformationGroup(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteInformationGroup();
        }

        protected void StoreInformationGroup_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreInformationGroup.DataSource = GetInformationGroup();
            StoreInformationGroup.DataBind();
        }

        protected void ExportExcelInformationGroup(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=InformationGroup.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteInformationGroup()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataInformationGroup()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataInformationGroup()
        {
            RowSelectionModel sm = this.GridPanelInformationGroup.SelectionModel.Primary as RowSelectionModel;
            string information_group_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                information_group_id = row.RecordID;
            }
            objInformationGroup.deleteData(information_group_id);
            StoreInformationGroup.DataSource = GetInformationGroup();
            StoreInformationGroup.DataBind();
        }



        protected void rowSelectInformationGroup(object sender, DirectEventArgs e)
        {
            txt_information_group_id.Text = e.ExtraParams["information_group_id"];
            objInformation.information_group_id = Convert.ToInt32(txt_information_group_id.Text);
            DataTable dt = objInformation.getByObject(objInformation);
            GridPanelInformation.Collapsed = false;
            StoreInformation.DataSource = dt;
            StoreInformation.DataBind();
        }

        //InformationGroup

        //Information

        protected DataTable GetInformation()
        {
            tcdc_mst_information objGetByGroup = new tcdc_mst_information();
            objGetByGroup.information_group_id = Convert.ToInt32(txt_information_group_id.Text);
            DataTable dt = objInformation.getByObject(objGetByGroup);
            return dt;
        }

        protected void AddInformation(object sender, DirectEventArgs e)
        {

            InformationManageAdd1.information_group_id_values = txt_information_group_id.Text;
            InformationManageAdd1.Show();
        }

        protected void UpdateInformation(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["InformationValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string information_id = string.Empty;
            string information_group_id = string.Empty;
            string information_desc = string.Empty;
            string sorting_information_no = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                information_id = row["information_id"].ToString();
                information_group_id = row["information_group_id"].ToString();
                information_desc = row["information_desc"].ToString();
                sorting_information_no = row["sorting_information_no"].ToString();
            }

            objInformation.information_id = Convert.ToInt32(information_id);
            objInformation.information_group_id = Convert.ToInt32(information_group_id);
            objInformation.information_desc = information_desc;
            objInformation.sorting_information_no = Convert.ToInt32(sorting_information_no);
            objInformation.updateData(objInformation);
            StoreInformation.DataSource = GetInformation();
            StoreInformation.DataBind();
        }

        protected void DeleteInformation(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteInformation();
        }

        protected void StoreInformation_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreInformation.DataSource = GetInformation();
            StoreInformation.DataBind();
        }

        protected void ExportExcelInformation(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=Information.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteInformation()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataInformation()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataInformation()
        {
            RowSelectionModel sm = this.GridPanelInformation.SelectionModel.Primary as RowSelectionModel;
            string information_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                information_id = row.RecordID;
            }
            objInformation.deleteData(information_id);
            StoreInformation.DataSource = GetInformation();
            StoreInformation.DataBind();
        }

        //Information
    }
}